package com.zhangli.config;

import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import com.zhangli.security.AES;
/**
 * Created by zhangli on 2017/8/20.
 */

public class Config {
    private final static String TAG = "PropertiesUtil";
    private final static String security_seed = "123rtgvbnmki87i7u6yt098i'[][p;loikujh876457ujhg/.,mn\\][p2ws65t";

    //private Context mContext;
    private String mPath;
    private String mFile;
    private Properties mProp;
    private static Config mPropUtil = null;

    public static Config getInstance() {
        if (mPropUtil == null) {
            mPropUtil = new Config();
            //mPropUtil.mContext = context;
            mPropUtil.mPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyToolKit";
            mPropUtil.mFile = "properties.ini";
            mPropUtil.load();
        }
        return mPropUtil;
    }

    public Config setPath(String path) {
        mPath = path;
        return this;
    }

    public Config setFile(String file) {
        mFile = file;
        return this;
    }

    private Config load() {
        Log.d(TAG, "load path="+mPath+"/"+mFile);
        try {
            File dir = new File(mPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File file = new File(dir, mFile);
            if (!file.exists()) {
                file.createNewFile();
            }
            InputStream is = new FileInputStream(file);
            mProp = new Properties();
            mProp.load(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    public void commit() {
        try {
            File file = new File(mPath + "/" + mFile);
            OutputStream os = new FileOutputStream(file);
            mProp.store(os, "");
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //mProp.clear();
    }

    public void writeString(String name, String value) {
        mProp.setProperty(name, value);
    }

    public String readString(String name, String defaultValue) {
        return mProp.getProperty(name, defaultValue);
    }

    public void writeInt(String name, int value) {
        mProp.setProperty(name, ""+value);
    }

    public int readInt(String name, int defaultValue) {
        return Integer.parseInt(mProp.getProperty(name, ""+defaultValue));
    }

    public void writeBoolean(String name, boolean value) {
        mProp.setProperty(name, ""+value);
    }

    public boolean readBoolean(String name, boolean defaultValue) {
        return Boolean.parseBoolean(mProp.getProperty(name, ""+defaultValue));
    }

    public void writeDouble(String name, double value) {
        mProp.setProperty(name, ""+value);
    }

    public double readDouble(String name, double defaultValue) {
        return Double.parseDouble(mProp.getProperty(name, ""+defaultValue));
    }

    public void writePassword(String name, String value) {
        String encrypt_value = "";
        try {
            encrypt_value = AES.encrypt(value,security_seed);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mProp.setProperty(name, encrypt_value);
    }

    public String readPassword(String name) {
        String encrypt_value = mProp.getProperty(name, "");
        String value = "";
        try {
            value = AES.decrypt(encrypt_value,security_seed);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
}
